package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.OmsOrderReturnReason;
import com.itheima.mapper.OmsOrderReturnReasonMapper;
import com.itheima.service.db.OmsOrderReturnReasonService;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@DubboService
public class OmsOrderReturnReasonServiceImpl implements OmsOrderReturnReasonService{
    @Autowired
    private OmsOrderReturnReasonMapper omsOrderReturnReasonMapper;

    @Override
    public PageBeanVo getPage(Integer pageNum, Integer pageSize) {
        Page<OmsOrderReturnReason> page=new Page<>(pageNum,pageSize);

        Page<OmsOrderReturnReason> reasonPage = omsOrderReturnReasonMapper.selectPage(page, new LambdaQueryWrapper<OmsOrderReturnReason>());
        return new PageBeanVo(pageNum,pageSize,reasonPage.getTotal(),reasonPage.getRecords());
    }

    @Override
    public R save(OmsOrderReturnReason omsOrderReturnReason) {
        List<OmsOrderReturnReason> omsOrderReturnReasons = omsOrderReturnReasonMapper.selectList(new LambdaQueryWrapper<>());
        for (OmsOrderReturnReason orderReturnReason : omsOrderReturnReasons) {
            if (omsOrderReturnReason.getName().equals(orderReturnReason.getName())){
                return R.error(500,"该理由已经存在");

            }
        }
        LocalDateTime dt = LocalDateTime.now();
        omsOrderReturnReason.setCreateTime(dt);
        omsOrderReturnReasonMapper.insert(omsOrderReturnReason);
        return R.success(null);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            omsOrderReturnReasonMapper.deleteById(id);
        }
    }

    @Override
    public void updateStatus(List<Long> ids, Integer status) {
        for (Long id : ids) {
            OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectById(id);
            omsOrderReturnReason.setStatus(status);
            omsOrderReturnReasonMapper.updateById(omsOrderReturnReason);
        }
    }

    @Override
    public OmsOrderReturnReason findById(Long id) {
        OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectById(id);
        return omsOrderReturnReason;
    }

    @Override
    public void update(Long id, OmsOrderReturnReason omsOrderReturnReason) {
        omsOrderReturnReason.setId(id);
        omsOrderReturnReasonMapper.updateById(omsOrderReturnReason);
    }
}
